<!DOCTYPE html>
<html xmlns:th="http://www.w3.org/1999/xhtml">

<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>类型模板管理</title>
    <!-- Tell the browser to be responsive to screen width -->
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <link rel="stylesheet" href="../plugins/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="../plugins/adminLTE/css/AdminLTE.css">
    <link rel="stylesheet" href="../plugins/adminLTE/css/skins/_all-skins.min.css">
    <link rel="stylesheet" href="../css/style.css">
    <script src="../plugins/jQuery/jquery-2.2.3.min.js"></script>
    <script src="../plugins/bootstrap/js/bootstrap.min.js"></script>
    <link rel="stylesheet" href="../plugins/select2/select2.css"/>
    <link rel="stylesheet" href="../plugins/select2/select2-bootstrap.css"/>
    <script src="../plugins/select2/select2.min.js" type="text/javascript"></script>

    <!--引入host文件中配置的域名   这是一个自定义的文件-->
    <script src="../js/pages/public.js"></script>
</head>

<body class="hold-transition skin-red sidebar-mini">
<!-- .box-body -->

<div class="box-header with-border">
    <h3 class="box-title">商品类型模板管理</h3>
</div>

<div class="box-body">

    <!-- 数据表格 -->
    <div class="table-box">

        <!--工具栏-->
        <div class="pull-left">
            <div class="form-group form-inline">
                <div class="btn-group">
                    <button type="button" class="btn btn-default" title="新建" data-toggle="modal"
                            data-target="#editModal"><i class="fa fa-file-o"></i> 新建
                    </button>
                    <button type="button" class="btn btn-default" onclick="DelteType()" title="删除"><i
                            class="fa fa-trash-o"></i> 删除
                    </button>

                    <button type="button" class="btn btn-default" title="刷新" onclick="window.location.reload();"><i
                            class="fa fa-refresh"></i> 刷新
                    </button>
                </div>
            </div>
        </div>
        <div class="box-tools pull-right">
            <div class="has-feedback">
                <form id="FormId">
                    <input style="width: 200px" name="name" class="form-control" placeholder="规格名称">
                </form>
                <button class="btn btn-default" onclick="findTypeList()">查询</button>
            </div>
        </div>
        <!--工具栏/-->

        <!--数据列表-->
        <table id="dataList" class="table table-bordered table-striped table-hover dataTable">
            <thead>
            <tr>
                <th class="" style="padding-right:0px">
                    <input id="selall" type="checkbox" class="icheckbox_square-blue">
                </th>
                <th class="sorting_asc">模板ID</th>
                <th class="sorting">分类模板名称</th>
                <th class="sorting">关联品牌</th>
                <th class="sorting">关联规格</th>
                <th class="sorting">扩展属性</th>
                <th class="text-center">操作</th>
            </tr>
            </thead>
            <tbody id="TypeTbodyId">
            <!--									  <tr>-->
            <!--			                              <td><input  type="checkbox"></td>			                              -->
            <!--				                          <td>2</td>-->
            <!--									      <td>服装</td>-->
            <!--									      <td>七匹狼,雅戈尔</td>-->
            <!--									      <td>服装尺码</td>									 -->
            <!--									      <td></td>		                                                                  -->
            <!--		                                  <td class="text-center">                                           -->
            <!--		                                 	  <button type="button" class="btn bg-olive btn-xs" data-toggle="modal" data-target="#editModal" >修改</button>                                           -->
            <!--		                                  </td>-->
            <!--			                          </tr>-->
            </tbody>
        </table>
        <!--数据列表/-->


    </div>
    <!-- 数据表格 /-->
    <form id="indexForm">
        <a href="javascript:toPage(3)">首页</a>
        <a href="javascript:toPage(1)">上一页</a>
        <a href="javascript:toPage(2)">下一页</a>
        <a href="javascript:toPage(4)">尾页</a>
        第<input type="text" name="pageNum" id="pageNum" size="1px" readonly="readonly">页
        共<input type="text" name="pages" id="pages" size="1px" disabled="disabled">页
        共<input type="text" name="total" id="total" size="1px" disabled="disabled">条
    </form>


</div>
<!-- /.box-body -->


<!-- 编辑窗口 -->
<div class="modal fade" id="editModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                <h3 id="myModalLabel">商品类型模板编辑</h3>
            </div>
            <div class="modal-body">

                <table class="table table-bordered table-striped" width="800px">
                    <tr>
                        <td>商品类型</td>
                        <td><input class="form-control" id="insertname" value="" placeholder="商品类型"></td>
                    </tr>
                    <tr>
                        <td>关联品牌</td>
                        <td>
                            <select id="IBrandSelectId" style="width: 100%" multiple></select>
                        </td>
                    </tr>
                    <tr>
                        <td>关联规格</td>
                        <td>
                            <select id="ISpecSelectId" style="width: 100%" multiple></select>
                        </td>
                    </tr>

                    <tr>
                        <td>扩展属性</td>
                        <td>
                            <div class="btn-group">
                                <button id="addType" type="button" class="btn btn-default" title="新增扩展属性"><i
                                        class="fa fa-file-o"></i> 新增扩展属性
                                </button>

                            </div>
                            <table id="cusIdinsert" class="table table-bordered table-striped" width="800px">
                                <thead>
                                <tr>
                                    <td><input type="checkbox" class="icheckbox_square-blue"></td>
                                    <td>属性名称</td>
                                    <td>操作</td>
                                </tr>
                                </thead>
                                <tbody id="addType1">
                                <!--                                <tr>-->
                                <!--                                    <td><input type="checkbox" class="icheckbox_square-blue"></td>-->
                                <!--                                    <td><input class="form-control" placeholder="属性名称"></td>-->
                                <!--                                    <td>-->
                                <!--                                        <button type="button" class="btn btn-default" title="删除"><i-->
                                <!--                                                class="fa fa-trash-o"></i> 删除-->
                                <!--                                        </button>-->
                                <!--                                    </td>-->
                                <!--                                </tr>-->
                                </tbody>
                            </table>

                        </td>
                    </tr>

                </table>

            </div>
            <div class="modal-footer">
                <button class="btn btn-success" data-dismiss="modal" aria-hidden="true" onclick="InsertType()">保存
                </button>
                <button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
            </div>
        </div>
    </div>
</div>


<!--修改窗口-->
<div class="modal fade" id="UpdateEditModal" tabindex="-1" role="dialog" aria-labelledby="UpdatemyModalLabel"
     aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                <h3 id="UpdatemyModalLabel">商品类型模板编辑</h3>
            </div>
            <div class="modal-body">

                <input type="hidden" id="updateId" name="id" value="id">
                <table class="table table-bordered table-striped" width="800px">
                    <tr>
                        <td>商品类型</td>
                        <td><input class="form-control" id="updatename" name="name" placeholder="商品类型"></td>
                    </tr>
                    <tr>
                        <td>关联品牌</td>
                        <td>
                            <select id="UBrandSelectId" style="width: 100%" multiple></select>
                        </td>
                    </tr>
                    <tr>
                        <td>关联规格</td>
                        <td>
                            <select id="USpecSelectId" style="width: 100%" multiple></select>
                        </td>
                    </tr>

                    <tr>
                        <td>扩展属性</td>
                        <td>
                            <div class="btn-group">
                                <button id="updateType" type="button" class="btn btn-default" title="新增扩展属性"><i
                                        class="fa fa-file-o"></i> 新增扩展属性
                                </button>

                            </div>
                            <table id="cusId" class="table table-bordered table-striped" width="800px">
                                <thead>
                                <tr>
                                    <td><input type="checkbox" class="icheckbox_square-blue"></td>
                                    <td>属性名称</td>
                                    <td>操作</td>
                                </tr>
                                </thead>
                                <tbody id="updateType1">
                                <!--                                    <tr>-->
                                <!--                                        <td><input type="checkbox" class="icheckbox_square-blue"></td>-->
                                <!--                                        <td><input class="form-control" placeholder="属性名称"></td>-->
                                <!--                                        <td>-->
                                <!--                                            <button type="button" class="btn btn-default" title="删除"><i-->
                                <!--                                                    class="fa fa-trash-o"></i> 删除-->
                                <!--                                            </button>-->
                                <!--                                        </td>-->
                                <!--                                    </tr>-->
                                </tbody>
                            </table>

                        </td>
                    </tr>

                </table>

            </div>
            <div class="modal-footer">
                <button class="btn btn-success" data-dismiss="modal" aria-hidden="true" onclick="TypeUpdate()">保存
                </button>
                <button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
            </div>
        </div>
    </div>
</div>

</body>
<script th:inline="javascript">
    $(function () {
        getTypeList();
        getBrandList();
        getSpecList();
    })

    function getTypeList() {
        $.ajax({
            url: domain + "/type/getTypeList",
            headers:{'Autherizetion':window.localStorage.getItem("token")},//获取浏览器缓存中的token进行过滤器放过
            // contentType: "application/json;charset=utf-8",//我将来要往前台发送数据的时候是以json的格式发送的
            dataType: "json",
            type: "post",//必须指定请求类型
            data: $("#indexForm").serialize(),//往后台发送的确确实实是一个json数据  JSON.stringify({})
            success: function (result) {
                if (result.code == 11) {
                    // console.log(result)
                    var data = result.data.rows;
                    var html = "";
                    for (let i = 0; i < data.length; i++) {
                        html += " <tr>\n" +
                            "\t\t\t                              <td><input  name='check' value='" + data[i].id + "' type=\"checkbox\"></td>\t\t\t                              \n" +
                            "\t\t\t\t                          <td>" + data[i].id + "</td>\n" +
                            "\t\t\t\t\t\t\t\t\t      <td>" + data[i].name + "</td>\n";
                        /*specIds*/
                        var specId = JSON.parse(data[i].specIds)/*json转对象*/
                        spec = '';
                        for (let j = 0; j < specId.length; j++) {
                            spec += ',' + specId[j].text;
                        }
                        specIds = spec.substring(1);
                        html += '<td>' + specIds + '</td>';

                        /*brandIds*/
                        var brand = JSON.parse(data[i].brandIds);
                        bran = '';
                        for (let j = 0; j < brand.length; j++) {
                            bran += ',' + brand[j].text;
                        }
                        brandIds = bran.substring(1);
                        html += '<td>' + brandIds + '</td>';

                        /*customAttributeItems*/
                        var custom = JSON.parse(data[i].customAttributeItems);
                        cus = '';
                        for (let j = 0; j < custom.length; j++) {
                            cus += ',' + custom[j].text;
                        }
                        customAttributeItems = cus.substring(1);
                        html += '<td>' + customAttributeItems + '</td>',
                            // "\t\t\t\t\t\t\t\t\t      <td>"+data[i].brandIds+"</td>\n" +
                            // "\t\t\t\t\t\t\t\t\t      <td>"+data[i].specIds+"</td>\t\t\t\t\t\t\t\t\t \n" +
                            //  "\t\t\t\t\t\t\t\t\t      <td></td>\t\t                                                                  \n" +
                            html += "\t\t                                  <td class=\"text-center\">                                           \n" +
                                "\t\t                                 \t  <button type=\"button\" onclick='toUpdate(" + data[i].id + ")' class=\"btn bg-olive btn-xs\" data-toggle=\"modal\" data-target=\"#UpdateEditModal\" >修改</button>                                           \n" +
                                "\t\t                                  </td>\n" +
                                "\t\t\t                          </tr>"
                        $("#TypeTbodyId").html(html)
                    }
                    $("#pageNum").val(result.data.pageNum)
                    $("#pages").val(result.data.pages)
                    $("#total").val(result.data.total)
                } else if (result.code == 12) {
                    top.window.location.href="../login.html";//token失效 跳转回登录界面  重新登录生成token  放进缓存  重新获取
                }
            }, error: function () {
                alert("模板管理查询失败")
            }
        })
    }

    //模糊查询
    function findTypeList() {
        var FormId = $("#FormId").serializeObject();//serializeObject 是自定义文件中自定义的一个方法
        $.ajax({
            url: domain + "/type/findTypeList",
            headers:{'Autherizetion':window.localStorage.getItem("token")},//获取浏览器缓存中的token进行过滤器放过
            contentType: "application/json;charset=utf-8",//我将来要往前台发送数据的时候是以json的格式发送的
            dataType: "json",
            type: "post",//必须指定请求类型
            data: JSON.stringify(FormId),//往后台发送的确确实实是一个json数据 JSON.stringify({})
            success: function (result) {
                if (result.code == 11) {
                    console.log(result)
                    var data = result.data;
                    var html = "";
                    for (let i = 0; i < data.length; i++) {
                        html += " <tr>\n" +
                            "\t\t\t                              <td><input name='check' value='" + data[i].id + "' type=\"checkbox\"></td>\t\t\t                              \n" +
                            "\t\t\t\t                          <td>" + data[i].id + "</td>\n" +
                            "\t\t\t\t\t\t\t\t\t      <td>" + data[i].name + "</td>\n";
                        /*specIds*/
                        var specId = JSON.parse(data[i].specIds)/*json转对象*/
                        spec = '';
                        for (let j = 0; j < specId.length; j++) {
                            spec += ',' + specId[j].text;
                        }
                        specIds = spec.substring(1);
                        html += '<td>' + specIds + '</td>';

                        /*brandIds*/
                        var brand = JSON.parse(data[i].brandIds);
                        bran = '';
                        for (let j = 0; j < brand.length; j++) {
                            bran += ',' + brand[j].text;
                        }
                        brandIds = bran.substring(1);
                        html += '<td>' + brandIds + '</td>';

                        /*customAttributeItems*/
                        var custom = JSON.parse(data[i].customAttributeItems);
                        cus = '';
                        for (let j = 0; j < custom.length; j++) {
                            cus += ',' + custom[j].text;
                        }
                        customAttributeItems = cus.substring(1);
                        html += '<td>' + customAttributeItems + '</td>',
                            html += "\t\t                                  <td class=\"text-center\">                                           \n" +
                                "\t\t                                 \t  <button type=\"button\" onclick='toUpdate(" + data[i].id + ")' class=\"btn bg-olive btn-xs\" data-toggle=\"modal\" data-target=\"#UpdateEditModal\" >修改</button>                                           \n" +
                                "\t\t                                  </td>\n" +
                                "\t\t\t                          </tr>"
                        $("#TypeTbodyId").html(html)
                    }
                } else if (result.code == 12) {
                    top.window.location.href="../login.html";//token失效 跳转回登录界面  重新登录生成token  放进缓存  重新获取
                }
            }, error: function () {
                alert("规格管理异常，请联系管理员")
            }
        })
    }

    /*分页*/
    function toPage(id) {
        var pageNum = parseInt($("#pageNum").val());
        var pages = parseInt($("#pages").val());
        if (id == 1) {
            if (pageNum <= 1) {
                alert("已是首页");
                return;
            }
            $("#pageNum").val(pageNum - 1);
        } else if (id == 2) {
            if (pageNum >= pages) {
                alert("已是尾页");
                return;
            }
            $("#pageNum").val(pageNum + 1);
        } else if (id == 3) {
            $("#pageNum").val(1);
        } else if (id == 4) {
            $("#pageNum").val(pages);
        }
        getTypeList();
    }

    function getBrandList() {
        $.ajax({
            url: domain + "/brand/getBrandListAndSelect2",
            headers:{'Autherizetion':window.localStorage.getItem("token")},//获取浏览器缓存中的token进行过滤器放过
            contentType: "application/json;charset=utf-8",//我将来要往前台发送数据的时候是以json的格式发送的
            dataType: "json",
            type: "post",//必须指定请求类型
            data: JSON.stringify({}),//往后台发送的确确实实是一个json数据  JSON.stringify({})
            success: function (result) {
                if (result.code == 11) {
                    console.log(result)
                    var data = result.data;
                    var html = "";
                    for (let i = 0; i < data.length; i++) {
                        html += "<option value='" + data[i].id + "'>" + data[i].name + "</option>"
                        $("#UBrandSelectId").html(html)
                        $("#UBrandSelectId").select2()
                        $("#IBrandSelectId").html(html)
                        $("#IBrandSelectId").select2()
                    }
                } else if (result.code == 12) {
                    top.window.location.href="../login.html";//token失效 跳转回登录界面  重新登录生成token  放进缓存  重新获取
                }
            }, error: function () {
                alert("模板管理查询失败")
            }
        })
    }

    function getSpecList() {
        $.ajax({
            url: domain + "/spec/getSpecListAndSelect2",
            headers:{'Autherizetion':window.localStorage.getItem("token")},//获取浏览器缓存中的token进行过滤器放过
            contentType: "application/json;charset=utf-8",//我将来要往前台发送数据的时候是以json的格式发送的
            dataType: "json",
            type: "post",//必须指定请求类型
            data: JSON.stringify({}),//往后台发送的确确实实是一个json数据  JSON.stringify({})
            success: function (result) {
                if (result.code == 11) {
                    // console.log(result)
                    var data = result.data;
                    var html = "";
                    for (let i = 0; i < data.length; i++) {
                        // console.log(data[i].id)
                        html += "<option value='" + data[i].id + "'>" + data[i].specName + "</option>"
                        $("#USpecSelectId").html(html)
                        $("#USpecSelectId").select2()
                        $("#ISpecSelectId").html(html)
                        $("#ISpecSelectId").select2()
                    }
                } else if (result.code == 12) {
                    top.window.location.href="../login.html";//token失效 跳转回登录界面  重新登录生成token  放进缓存  重新获取
                }
            }, error: function () {
                alert("模板管理查询失败")
            }
        })
    }

    //回显   根据id查询模板信息
    function toUpdate(id) {
        // $('input[name="InsertspecName"]').val();
        // console.log("回显的id ↓")
        // console.log(id)
        $.ajax({
            url: domain + "/type/getTypeById",
            headers:{'Autherizetion':window.localStorage.getItem("token")},//获取浏览器缓存中的token进行过滤器放过
            contentType: "application/json;charset=utf-8",//我将来要往前台发送数据的时候是以json的格式发送的
            dataType: "json",
            type: "get",//必须指定请求类型
            data: {id: id},//往后台发送的确确实实是一个json数据  JSON.stringify({})
            success: function (result) {
                if (result.code == 11) {
                    // console.log(result)

                    //回显商品类型
                    var typeId = $("#updateId").val(result.data.id);
                    var typename = $("#updatename").val(result.data.name);

                    //品牌回显
                    var data = result.data.brandIds;
                    var brandlist = JSON.parse(data);
                    var ids = [];
                    for (let i = 0; i < brandlist.length; i++) {
                        ids.push(brandlist[i].id)
                    }
                    // console.log(ids)
                    $("#UBrandSelectId").val(ids).trigger("change")

                    //规格回显
                    var data1 = result.data.specIds;
                    var specList = JSON.parse(data1);
                    var ids1 = [];
                    for (let i = 0; i < specList.length; i++) {
                        ids1.push(specList[i].id)
                    }
                    $("#USpecSelectId").val(ids1).trigger("change")

                    //扩展属性
                    var item = result.data.customAttributeItems;
                    var itemlist = JSON.parse(item);
                    var spec = "";
                    for (let i = 0; i < itemlist.length; i++) {
                        spec += "<tr>\n" +
                            "                                        <td><input type=\"checkbox\" class=\"icheckbox_square-blue\"></td>\n" +
                            "                                        <td><input class=\"form-control\"  value='" + itemlist[i].text + "' placeholder=\"属性名称\"></td>\n" +
                            "                                        <td>\n" +
                            "                                            <button type=\"button\" class=\"btn btn-default\" onclick='delTr(this)' title=\"删除\"><i\n" +
                            "                                                    class=\"fa fa-trash-o\"></i> 删除\n" +
                            "                                            </button>\n" +
                            "                                        </td>\n" +
                            "                                    </tr>";
                    }
                    $("#updateType1").html(spec)
                } else if (result.code == 12) {
                    top.window.location.href="../login.html";//token失效 跳转回登录界面  重新登录生成token  放进缓存  重新获取
                }
            }, error: function () {
                alert("模板管理查询失败")
            }
        })
    }

    /*button的id*/
    $("#addType").click(function () {
        var tr = "                                <tr>\n" +
            "                                    <td><input type=\"checkbox\" class=\"icheckbox_square-blue\"></td>\n" +
            "                                    <td><input class=\"form-control\" placeholder=\"属性名称\"></td>\n" +
            "                                    <td>\n" +
            "                                        <button type=\"button\" onclick='delTr(this)' class=\"btn btn-default\" title=\"删除\"><i\n" +
            "                                                class=\"fa fa-trash-o\"></i> 删除\n" +
            "                                        </button>\n" +
            "                                    </td>\n" +
            "                                </tr>";
        $("#addType1").append(tr);/*追加一行，问题：格式不同*/
        /*tbody的id*/
    });

    $("#updateType").click(function () {
        var tr = "                                <tr>\n" +
            "                                    <td><input type=\"checkbox\" class=\"icheckbox_square-blue\"></td>\n" +
            "                                    <td><input class=\"form-control\" placeholder=\"属性名称\"></td>\n" +
            "                                    <td>\n" +
            "                                        <button type=\"button\" onclick='delTr(this)' class=\"btn btn-default\" title=\"删除\"><i\n" +
            "                                                class=\"fa fa-trash-o\"></i> 删除\n" +
            "                                        </button>\n" +
            "                                    </td>\n" +
            "                                </tr>";
        $("#updateType1").append(tr);/*追加一行，问题：格式不同*/
        /*tbody的id*/
    });

    function delTr(nowTr) {
        /*
        * button的父级是td，td的父级是tr所以parent.parent
        * 删除当前行
        * 多一个parent就代表向前一个标签
        * 如果多一个parent就会删除整个table
        * */
        $(nowTr).parent().parent().remove()
    }

    //修改2
    function TypeUpdate() {
        //前端控制台凡是三角号的全是对象   如果是json字符串是没有三角号的

        //模板id
        var id1 = $("#updateId").val()
        //-------------------------------模板名称----------------------
        var name1 = $("#updatename").val();

        //-------------------------------品牌-------------------------
        var brandlist = $("#UBrandSelectId").select2("data")
        var speclist = $("#USpecSelectId").select2("data")
        var brandIds1 = [];
        for (let i = 0; i < brandlist.length; i++) {
            var temp = {"id": brandlist[i].id, "text": brandlist[i].text}
            brandIds1.push(temp)
        }
        var brandIds = JSON.stringify(brandIds1)
        console.log(JSON.stringify(brandIds1))

        //--------------------------------规格-------------------------
        var specIds1 = [];
        for (let i = 0; i < speclist.length; i++) {
            var temp1 = {"id": speclist[i].id, "text": speclist[i].text}
            specIds1.push(temp1)
        }
        var specIds = JSON.stringify(specIds1);
        console.log(JSON.stringify(specIds1))

        //----------------------------扩展属性----------------------------------
        /*扩展属性*/
        var trList = $("#updateType1").find('tr');/*这里有一个错误：id给错了所以拿不到table里的值*/
        var customList = [];
        for (let i = 0; i < trList.length; i++) {
            var tdList = $(trList[i]).find('td');
            var customAttributeItems = $(tdList[1]).find('input').val();
            var custom = {"text": customAttributeItems}
            customList.push(custom);
        }
        var customjson = JSON.stringify(customList);
        console.log(customjson)
        var list = {
            "id": id1,
            "name": name1,
            "brandIds": brandIds,
            "specIds": specIds,
            "customAttributeItems": customjson
        };
        console.log(list)
        //注意：不能push  因为现在获取到的值都是一个个小的集合  相当于集合嵌套
        $.ajax({
            url: domain + "/type/updateType",
            headers:{'Autherizetion':window.localStorage.getItem("token")},//获取浏览器缓存中的token进行过滤器放过
            contentType: "application/json;charset=utf-8",//我将来要往前台发送数据的时候是以json的格式发送的
            dataType: "json",
            type: "put",//必须指定请求类型
            data: JSON.stringify(list),//往后台发送的确确实实是一个json数据   JSON.stringify({})
            success: function (result) {
                if (result.code == 11) {
                    console.log(result)
                    alert("修改成功")
                } else if (result.code == 12) {
                    top.window.location.href="../login.html";//token失效 跳转回登录界面  重新登录生成token  放进缓存  重新获取
                }
            }, error: function () {
                alert("规格管理异常，请联系管理员")
            }
        })
    }

    //增加
    function InsertType() {
        //-------------------------------品牌-------------------------
        var brandlist = $("#IBrandSelectId").select2("data")
        var speclist = $("#ISpecSelectId").select2("data")
        var brandIds1 = [];
        for (let i = 0; i < brandlist.length; i++) {
            var temp = {"id": brandlist[i].id, "text": brandlist[i].text}
            brandIds1.push(temp)
        }
        var brandIds = JSON.stringify(brandIds1)
        console.log(JSON.stringify(brandIds))

        //--------------------------------规格-------------------------
        var specIds1 = [];
        for (let i = 0; i < speclist.length; i++) {
            var temp1 = {"id": speclist[i].id, "text": speclist[i].text}
            specIds1.push(temp1)
        }
        var specIds = JSON.stringify(specIds1);
        console.log(JSON.stringify(specIds1))

        /*扩展属性*/
        var trList = $("#addType1").find('tr');/*这里有一个错误：id给错了所以拿不到table里的值*/
        var customList = [];
        $(trList).each(function (i, e) {
            var temp = {};
            var tdList = $(e).find('td');
            //获取每一列
            temp["text"] = $(tdList[1]).find('input').val();//存入List集合中
            customList.push(temp);
        })
        var customjson = JSON.stringify(customList);
        console.log(customjson)
        var list = {
            "name": $("#insertname").val(),
            "brandIds": brandIds,
            "specIds": specIds,
            "customAttributeItems": customjson
        };

        console.log(list)
        $.ajax({
            url: domain + "/type/InsertType",
            headers:{'Autherizetion':window.localStorage.getItem("token")},//获取浏览器缓存中的token进行过滤器放过
            contentType: "application/json;charset=utf-8",//我将来要往前台发送数据的时候是以json的格式发送的
            dataType: "json",
            type: "post",//必须指定请求类型
            data: JSON.stringify(list),//往后台发送的确确实实是一个json数据   JSON.stringify({})
            success: function (result) {
                if (result.code == 11) {
                    console.log(result)
                    alert("增加成功")
                } else if (result.code == 12) {
                    top.window.location.href="../login.html";//token失效 跳转回登录界面  重新登录生成token  放进缓存  重新获取
                }
            }, error: function () {
                alert("模板管理异常，请联系管理员")
            }
        })
    }

    //批量删除
    function DelteType() {
        var arr = [];
        var check = document.getElementsByName("check");
        for (let i = 0; i < check.length; i++) {
            if (check[i].checked) {
                arr.push(check[i].value)
            }
        }
        console.log(arr)
        if (confirm("确认要删除吗？")) {
            $.ajax({
                url: domain + "/type/deleteType/" + arr,
                headers:{'Autherizetion':window.localStorage.getItem("token")},//获取浏览器缓存中的token进行过滤器放过
                dcontentType: "application/json;charset=utf-8",//我将来要往前台发送数据的时候是以json的格式发送的
                dataType: "json",
                type: "delete",//必须指定请求类型
                // data:{arr:arr},//往后台发送的确确实实是一个json数据
                success: function (result) {
                    if (result.code == 11) {
                        alert("删除成功")
                    } else if (result.code == 12) {
                        top.window.location.href="../login.html";//token失效 跳转回登录界面  重新登录生成token  放进缓存  重新获取
                    }
                }
            })
        }
    }
</script>
</html>